<template>
  <div class="search">
    <van-nav-bar fixed title="商品列表" left-arrow @click-left="$router.go(-1)" />

    <van-search readonly shape="round" background="#ffffff" v-model="keyword" show-action placeholder="请输入商品"
      @click="$router.push('/search')">
      <template #action>
        <van-icon class="tool" name="apps-o" />
      </template>
    </van-search>

    <!-- 排序选项按钮 -->
    <van-dropdown-menu>
      <van-dropdown-item v-model="sortType" :options="option1" />
      <van-dropdown-item v-model="sortPrice" :options="option2" />
    </van-dropdown-menu>
    <!-- <div class="sort-btns">
      <div class="sort-item">综合</div>
      <div class="sort-item">销量</div>
      <div class="sort-item">价格 </div>
    </div> -->

    <div class="goods-list" v-if="goodsList !== null">
      <GoodsItem v-for="item in goodsList" :key="item.goods_id" :item="item"></GoodsItem>
    </div>
  </div>
</template>

<script>
import getSearchResult from '@/api/search'
import GoodsItem from '@/components/GoodsItem.vue'
export default {
  name: 'SearchResult',
  components: {
    GoodsItem
  },
  data () {
    return {
      goodsList: [],
      keyword: '',
      sortType: 'all', // all-按综合搜索(默认)，sales-按销量搜索，price-按价格搜索
      sortPrice: 1, // 0-价格从低到高， 1-价格从高到低
      categoryId: 0,
      option1: [
        { text: '综合搜索', value: 'all' },
        { text: '销量搜索', value: 'sales' },
        { text: '价格搜索', value: 'price' }
      ],
      option2: [
        { text: '价格从低到高', value: 0 },
        { text: '价格从高到低', value: 1 }
      ]
    }
  },
  methods: {
    async getSearchResult () {
      const result = await getSearchResult(this.sortType, this.sortPrice, this.categoryId, this.keyword, 1)
      this.goodsList = result.data.list.data
    }
  },
  async created () {
    this.keyword = this.$route.query.keyword
    this.categoryId = this.$route.query.categoryId
    this.getSearchResult()
  },
  watch: {
    sortType: {
      handler () {
        this.getSearchResult()
      }
    },
    sortPrice: {
      handler () {
        this.getSearchResult()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.search {
  padding-top: 46px;

  ::v-deep .van-icon-arrow-left {
    color: #333;
  }

  .tool {
    font-size: 24px;
    height: 40px;
    line-height: 40px;
  }

  .sort-btns {
    display: flex;
    height: 36px;
    line-height: 36px;

    .sort-item {
      text-align: center;
      flex: 1;
      font-size: 16px;
    }
  }
}

// 商品样式
.goods-list {
  background-color: #f6f6f6;
}
</style>
